package com.wangyb.multitenant.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.wangyb.multitenant.vo.MtTemplateVo;
import com.wangyb.multitenant.bo.MtTemplateQueryBo;
import com.wangyb.multitenant.bo.MtTemplateAddBo;
import com.wangyb.multitenant.bo.MtTemplateEditBo;
import com.wangyb.multitenant.service.IMtTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 多租户模板Controller
 *
 * @author wangyb
 * @date 2021-06-23
 */
@Api(value = "多租户模板控制器", tags = {"多租户模板管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/multitenant/template")
public class MtTemplateController extends BaseController {

    private final IMtTemplateService iMtTemplateService;

	@Autowired
	private ISysMenuService menuService;

	@Autowired
	private TokenService tokenService;

    /**
     * 查询多租户模板列表
     */
    @ApiOperation("查询多租户模板列表")
    @PreAuthorize("@ss.hasPermi('multitenant:template:list')")
    @GetMapping("/list")
    public TableDataInfo<MtTemplateVo> list(@Validated MtTemplateQueryBo bo) {
        return iMtTemplateService.queryPageList(bo);
    }

    /**
     * 导出多租户模板列表
     */
    @ApiOperation("导出多租户模板列表")
    @PreAuthorize("@ss.hasPermi('multitenant:template:export')")
    @Log(title = "多租户模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MtTemplateVo> export(@Validated MtTemplateQueryBo bo) {
        List<MtTemplateVo> list = iMtTemplateService.queryList(bo);
        ExcelUtil<MtTemplateVo> util = new ExcelUtil<MtTemplateVo>(MtTemplateVo.class);
        return util.exportExcel(list, "多租户模板");
    }

    /**
     * 获取多租户模板详细信息
     */
    @ApiOperation("获取多租户模板详细信息")
    @PreAuthorize("@ss.hasPermi('multitenant:template:query')")
    @GetMapping("/{id}")
    public AjaxResult<MtTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iMtTemplateService.queryById(id));
    }

    /**
     * 新增多租户模板
     */
    @ApiOperation("新增多租户模板")
    @PreAuthorize("@ss.hasPermi('multitenant:template:add')")
    @Log(title = "多租户模板", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MtTemplateAddBo bo) {
        return toAjax(iMtTemplateService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改多租户模板
     */
    @ApiOperation("修改多租户模板")
    @PreAuthorize("@ss.hasPermi('multitenant:template:edit')")
    @Log(title = "多租户模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MtTemplateEditBo bo) {
        return toAjax(iMtTemplateService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除多租户模板
     */
    @ApiOperation("删除多租户模板")
    @PreAuthorize("@ss.hasPermi('multitenant:template:remove')")
    @Log(title = "多租户模板" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMtTemplateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 加载对应租户模板菜单列表树
	 */
	@GetMapping(value = "/templateMenuTreeselect/{templateId}")
	public AjaxResult templateMenuTreeselect(@PathVariable("templateId") Long templateId)
	{
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		List<SysMenu> menus = menuService.selectMenuList(loginUser.getUser().getUserId());
		Map<String,Object> ajax = new HashMap<>();
		ajax.put("checkedKeys", iMtTemplateService.selectMenuListByTemplateId(templateId));
		ajax.put("menus", menuService.buildMenuTreeSelect(menus));
		return AjaxResult.success(ajax);
	}
}
